package com.woollen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woollen.common.BaseResponse;
import com.woollen.common.ResultUtils;
import com.woollen.model.Hotel;
import com.woollen.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/hotel")
public class HotelController {
    @Autowired
    private HotelService hotelService;

    @PostMapping("/add")
    public BaseResponse<Long> add(@RequestBody Hotel hotel) {
        hotelService.save(hotel);
        return ResultUtils.success(hotel.getHotelId().longValue());
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody Hotel hotel) {
        return ResultUtils.success(hotelService.updateById(hotel));
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> delete(@RequestParam Integer id) {
        return ResultUtils.success(hotelService.removeById(id));
    }

    @GetMapping("/list")
    public BaseResponse<List<Hotel>> list() {
        return ResultUtils.success(hotelService.list());
    }

    @GetMapping("/page")
    public BaseResponse<Page<Hotel>> page(@RequestParam int page, @RequestParam int size, @RequestParam(required = false) String name) {
        QueryWrapper<Hotel> query = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            query.like("name", name);
        }
        Page<Hotel> hotelPage = hotelService.page(new Page<>(page, size), query);
        return ResultUtils.success(hotelPage);
    }

    @GetMapping("/detail")
    public BaseResponse<Hotel> detail(@RequestParam Integer id) {
        return ResultUtils.success(hotelService.getById(id));
    }
} 